import {
    getLyric,
    getSongDetail
} from "@/services";
import {
    fomatLyric
} from "@/utils/fomatLyric";
import {
    mathRandom
} from "@/utils/mathRandom";
import {
    createAsyncThunk,
    createSlice
} from "@reduxjs/toolkit";

// 网络请求
export const fetchPlayerAction = createAsyncThunk("fetchPlay", (payload, {
    dispatch,
    getState
}) => {
    // 1、根据id查找playlist中是否已经有了该歌曲
    const playList = getState().player.playList
    const songIndex = playList.findIndex(song => song.id === payload)
    // 判断是否找到歌曲
    if (songIndex !== -1) {
        dispatch(changeCurrentSongIndex(songIndex))
        const song = playList[songIndex]
        dispatch(changeSongDetails(song))
        // 获取歌词
        dispatch(fetchLyricAction(song.id))
    } else {
        // 没有找到歌曲处理
        getSongDetail(payload).then(res => {
            const song = res.songs && res.songs[0]
            if (!song) return
            // 将最新请求到的歌曲添加到播放播放列表中
            const newPlayList = [...playList]
            newPlayList.push(song)
            // 更新redux中的值
            dispatch(changePlayList(newPlayList))
            dispatch(changeCurrentSongIndex(newPlayList.length - 1))
            dispatch(changeSongDetails(song))

            // 获取歌词
            dispatch(fetchLyricAction(song.id))
        })
    }


})

// 切换歌曲
export const changeCurrentIndexAndSongAction = createAsyncThunk("changeCurrentIndexAndSong", (payload, {
    dispatch,
    getState
}) => {
    // 获取列表
    const playList = getState().player.playList
    // console.log(playList);
    // 获取当前的播放方式  0、循环  1、随机  2、单曲
    const sequence = getState().player.sequence
    // 获取歌曲在数组中的索引
    let currentSongIndex = getState().player.currentSongIndex

    // 判断逻辑
    switch (sequence) {
        case 1: //随机播放
            let randomIndex = mathRandom(playList.length)
            while (randomIndex === currentSongIndex) {
                randomIndex = mathRandom(playList.length)
            }
            currentSongIndex = randomIndex
            break;
        default: //顺序播放
            currentSongIndex += payload
            if (currentSongIndex >= playList.length) {
                currentSongIndex = 0
            }
            if (currentSongIndex < 0) {
                currentSongIndex = playList.length - 1
            }
    }
    const currentSong = playList[currentSongIndex]
    dispatch(changeSongDetails(currentSong))
    dispatch(changeCurrentSongIndex(currentSongIndex))

    // 获取歌词
    dispatch(fetchLyricAction(currentSong.id))
})

// 获取歌词
export const fetchLyricAction = createAsyncThunk("fetchLyric", (payload, {
    dispatch
}) => {
    getLyric(payload).then(res => {
        const lyric = res.lrc.lyric
        let lyricList = fomatLyric(lyric)
        dispatch(changeLyricList(lyricList))
    })
})

const playerSlice = createSlice({
    name: "player",
    initialState: {
        // 歌曲播放列表
        playList: [],
        // 歌曲在数组中的索引
        currentSongIndex: 0,
        // 被点击歌曲的数据
        songDetails: {},
        // 控制播放方式： 0、循环  1、随机  2、单曲
        sequence: 0,
        // 获取歌词数据
        lyricList: []
    },
    reducers: {
        // 歌曲播放列表
        changePlayList(state, {
            payload
        }) {
            state.playList = payload
        },
        // 歌曲在数组中的索引
        changeCurrentSongIndex(state, {
            payload
        }) {
            state.currentSongIndex = payload
        },
        // 被点击歌曲的数据
        changeSongDetails(state, {
            payload
        }) {
            state.songDetails = payload
        },
        // 控制播放方式： 0、循环  1、随机  2、单曲
        changeSequence(state, {
            payload
        }) {
            state.sequence = payload
        },
        // 获取歌词
        changeLyricList(state, {
            payload
        }) {
            state.lyricList = payload
        }
    }
})

export const {
    changePlayList,
    changeCurrentSongIndex,
    changeSongDetails,
    changeSequence,
    changeLyricList
} = playerSlice.actions

export default playerSlice.reducer